Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mobx

Package Overview
Dependencies
Maintainers
3
Versions
251
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mobx

Simple, scalable state management.

  • 3.6.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.7M
decreased by-0.79%
Maintainers
3
Weekly downloads
 
Created

What is mobx?

MobX is a state management library for JavaScript applications. It enables developers to manage the application state outside of UI frameworks in a reactive way that ensures that the state is consistent and predictable. MobX uses observable data structures and automatically tracks changes, updating the UI when necessary.

What are mobx's main functionalities?

Observable State

Create observable state that can be tracked and updated. When the state changes, MobX will automatically propagate changes to any computed values or reactions that depend on the changed state.

import { observable } from 'mobx';

const appState = observable({
  count: 0,
  increment: function() {
    this.count++;
  },
  decrement: function() {
    this.count--;
  }
});

Computed Values

Define computed values that will be re-evaluated when any observable data they depend on changes. Computed values are cached and only updated when necessary.

import { computed, makeObservable } from 'mobx';

class TodoList {
  todos = [];
  get unfinishedTodoCount() {
    return this.todos.filter(todo => !todo.finished).length;
  }
  constructor() {
    makeObservable(this, {
      todos: observable,
      unfinishedTodoCount: computed
    });
  }
}

Reactions

Reactions are a way to automatically run side effects when observable data changes. The autorun function is one type of reaction that runs immediately and then re-runs every time its dependencies change.

import { observable, autorun } from 'mobx';

const temperature = observable.box(20);

const reaction = autorun(() => {
  console.log(`Temperature is: ${temperature.get()}C`);
});

temperature.set(25); // This will trigger the autorun and log the new temperature

Actions

Actions are functions that modify observables. They are the only way to modify state in MobX, and they can be bound to the class instance using the action.bound decorator.

import { observable, action } from 'mobx';

class Store {
  @observable count = 0;

  @action.bound increment() {
    this.count++;
  }

  @action.bound decrement() {
    this.count--;
  }
}

Other packages similar to mobx

Keywords

FAQs

Package last updated on 12 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc